<?php
/**
 * huhaiqin,2025.2.5
 * 接口需要的token
 */
namespace Common\Model;
use Think\Model;

class TokenModel extends Model{

    /**
     * huhaiqin,2025.2.5
     * 生成token
     */
    public function create_token($uid = 0,$expireTime = 3600){
        $token = md5($uid.time().mt_rand(0,999999));
        $expireTime = time() + $expireTime;
        $data = [
            'uid' => $uid,
            'token' => $token,
            'expire_time' => $expireTime
        ];
        if($this->add($data)){
            return $token;
        }else{
            return false;
        }
    }

    /**
     * huhaiqin,2025.2.5
     * 检测token是否正确,如果验证成功，返回uid
     */
    public function check_token($token = ''){
        if($token == ''){
            return false;
        }
        $where = [
            'token' => $token,
            'expire_time' => ['gt',time()]
        ];
        $result = $this->where($where)->find();
        if($result){
            return $result['uid'];
        }else{
            return false;
        }
    }
}